package com.googlecode.continuity.core.server.hibernate;

import net.sf.hibernate4gwt.core.LazyKiller;
import net.sf.hibernate4gwt.core.beanlib.clone.CloneBeanReplicator;
import net.sf.hibernate4gwt.core.beanlib.IClassMapper;
import net.sf.beanlib.hibernate.HibernateBeanReplicator;

/**
 * CustomLazyKiller is a {@link LazyKiller} implementation that can also handle {@link Enum} objects.
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class CustomLazyKiller extends LazyKiller {
  private IClassMapper classMapper;

  /**
   * Default constructor, building a LazyKiller without class mapper.
   */
  public CustomLazyKiller() {
    this(null);
  }

  /**
   * Constructs a new CustomLazyKiller with the given class mapper.
   *
   * @param classMapper class mapper to be used by this LazyKiller
   */
  public CustomLazyKiller(IClassMapper classMapper) {
    super(classMapper);
    setClassMapper(classMapper);
  }

  /**
   * Clones a POJO controlled by hibnerate, creating an object of the <code>cloneClass</code> type.
   * @param hibernatePojo object to be cloned
   * @param cloneClass target class
   * @return clone object
   */
  protected Object clone(Object hibernatePojo, Class cloneClass) {
    HibernateBeanReplicator replicator = new CloneBeanReplicatorEnum(classMapper);

		return replicator.copy(hibernatePojo, cloneClass);
  }

  /**
   * Sets a new class mapper for this LazyKiller.
   *
   * @param classMapper new class mapper
   */
  public void setClassMapper(IClassMapper classMapper) {
    this.classMapper = classMapper;
    super.setClassMapper(classMapper);
  }
}
